import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Observable} from 'rxjs';

export interface MenuItem {
  ID: number;
  MENUORDER: number;
  NAME: string;
  PARENTID: number;
  VALUEINFO: string;
}

export interface MenuStructure {
  children: MenuItem[];
  root: MenuItem;
}

@Injectable({
  providedIn: 'root'
})
export class MymenuService {
  private myMenu: Observable<MenuStructure[]> | undefined;
  private myMenuReady: MenuStructure[] = [];

  constructor(private httpClient: HttpClient) {
  }

  getMenuData(paraUrl:string): MenuStructure[] {
    if (localStorage.getItem('jwt') != null) {
      console.log(this.myMenuReady)
      if (this.myMenuReady.length===0) {
        console.log('good----------------------------------');
        this.httpClient.post<MenuStructure[]>(paraUrl, null).subscribe(
          (data) => {
            console.log("进入：" + data)
            this.myMenuReady = data;
          }
        );
        console.log(this.myMenuReady);
      }
      return this.myMenuReady!
    }else{
      console.log("没有resource")
      return [];
    }
  }
}
